package com.mz.ozy.widget.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.mz.ozy.R;

/**
 * @author crazyZhangxl on 2018/10/25.
 * Describe:
 */

public class BottomPhotoDialog extends DialogFragment implements View.OnClickListener {

    private final String isBlack;
    //    private BottomSheetBehavior behavior;
    private OnPhotoListener listener;

    public BottomPhotoDialog(String isBlack) {
        this.isBlack = isBlack;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View rootView = inflater.inflate(R.layout.friend_more_layout, container, false);
        TextView tvJoinBlack = rootView.findViewById(R.id.dialog_image_clicked_btn_delete);
        TextView tvReport = rootView.findViewById(R.id.dialog_image_clicked_btn_report);
        TextView tvCancel = rootView.findViewById(R.id.dialog_image_clicked_btn_cancel);
        if ("1".equals(isBlack)) {
            tvJoinBlack.setText("移出黑名单");
        }
        tvJoinBlack.setOnClickListener(this);
        tvReport.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        if (window != null) {
            // 一定要设置Background，如果不设置，window属性设置无效
            window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
            DisplayMetrics dm = new DisplayMetrics();
            if (getActivity() != null) {
                WindowManager windowManager = getActivity().getWindowManager();
                if (windowManager != null) {
                    windowManager.getDefaultDisplay().getMetrics(dm);
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.gravity = Gravity.BOTTOM;
                    // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//                    params.height = height;
                    params.width = dm.widthPixels; //设置宽度
                    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//                            dm.heightPixels; //设置高度
                    window.setAttributes(params);
                }
            }
        }
    }

    public void setOnPhotoListener(OnPhotoListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        listener.onClick(v.getId(),isBlack);
        dismiss();
    }

    public interface OnPhotoListener {
        void onClick(int id, String isBlack);
    }
}

